// pages/mine/settings/username/username.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:""
  },
  onLoad:function(options){
    this.setData({
      value: options.name
    })
  },
  /**
   * 数据更改
   */
  onChange(e) {
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail
    })
  },
  /**
   * 确认修改
   */
  confirm(){
    console.log(this.data.value)
    app.globalData.request("post","/applet/user_login/editName",{
      name:this.data.value,
      id:wx.getStorageSync("id"),
      token:wx.getStorageSync("token")
    })
    .then(res => {
      wx.showToast({
        title: res.msg,
        icon:"success"
      })
      // 将用户名保存至缓存中
      let userData = wx.getStorageSync('userInfo')
      userData.nickName = this.data.value
      wx.setStorageSync('userInfo', userData)

      wx.showToast({
        title: '跳转中',
        icon: 'loading'
      })
      setTimeout(_ => {
        wx.navigateBack({
          delta: 1
        })
      },2000)
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: "none"
      })
    })
  }
})